﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyHocPhi.GUI.HeThong;
using QuanLyHocPhi.Model;
using QuanLyHocPhi.BLL;
using QuanLyHocPhi.GUI.DuLieu;

namespace QuanLyHocPhi.GUI
{
    using Model;
    using QuanLyHocPhi.GUI.TacNghiep;
    public partial class FormMain : Form
    {


        public FormMain()
        {
            InitializeComponent();
        }

        #region Đăng nhập
        private void OnDangNhapClick(object sender, EventArgs e)
        {
            DangNhap();
        }

        public void DangNhap()
        {
            FormDangNhap frm = new FormDangNhap();
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Program.NguoiSuDung = frm.NguoiSuDung;
                //if (Program.HocKy == null)
                //    ChonHocKyLamViec();
                if (frm.NguoiSuDung.VaiTro == 2)
                {
                    //backUpToolStripMenuItem.Enabled = true;
                    //backUpToolStripMenuItem.Visible = true;
                    menuHeThong_QuanLyNguoiSuDung.Enabled = true;
                }
            }
        }
        #endregion

        #region Đăng xuất
        private void OnDangXuatClick(object sender, EventArgs e)
        {
            DangXuat();
        }

        public void DangXuat()
        {
            Program.NguoiSuDung = null;
            backUpToolStripMenuItem.Enabled = false;
            backUpToolStripMenuItem.Visible = false;
            //Program.HocKy = null;
        }
        #endregion

        #region Thoát chương trình
        private void OnMenuHeThong_ThoatClick(object sender, EventArgs e)
        {
            ThoatChuongTrinh();
        }

        public void ThoatChuongTrinh()
        {
            this.Close();
        }
        #endregion

        private void OnLoad(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            Program.NguoiSuDung = null;
            //Program.HocKy = null;
            this.DangNhap();
        }

        private void OnMenuHeThong_DoiMatKhauClick(object sender, EventArgs e)
        {
            FormDoiMatKhau frm = new FormDoiMatKhau(Program.NguoiSuDung);
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                DialogUtils.ShowMessage("Đổi mật khẩu thành công!");
            frm = null;
        }

        private void OnMenuDuLieu_NamHocHocKyClick(object sender, EventArgs e)
        {
            FormHocKy frm = new FormHocKy();
            frm.ShowDialog();
            frm = null;
        }

        /// <summary>
        /// Cập nhật hiển thị người sử dụng đang làm việc
        /// </summary>
        internal void UpdateNguoiSuDung()
        {
            if (Program.NguoiSuDung == null)
            {
                #region Huỷ đăng nhập
                menuHeThong_DangNhap.Visible =
                    btnDangNhap.Visible = true;
                menuHeThong_DangXuat.Visible =
                    btnDangXuat.Visible =
                    menuDuLieu.Visible =
                    menuTacNghiep.Visible =
                    menuHeThong_QuanLyNguoiSuDung.Visible =
                    menuHeThong_DoiMatKhau.Visible = false;
                lblSinhVien_Title.Visible =
                    txtSinhVien_Keyword.Visible =
                    btnSinhVien_Search.Visible = false;

                lblStatus.Text = "Chưa đăng nhập";
                #endregion
            }
            else
            {
                #region Thiết lập thông tin người sử dụng
                menuHeThong_DangNhap.Visible =
                    btnDangNhap.Visible = false;
                menuHeThong_DangXuat.Visible =
                    btnDangXuat.Visible =
                    menuDuLieu.Visible =
                    menuTacNghiep.Visible =
                    menuHeThong_QuanLyNguoiSuDung.Visible =
                    menuHeThong_DoiMatKhau.Visible = true;
                lblSinhVien_Title.Visible = txtSinhVien_Keyword.Visible = btnSinhVien_Search.Visible = true;

                lblStatus.Text = string.Format("Đã đăng nhập vai trò {1} - Tên đăng nhập: {0}",
                    Program.NguoiSuDung.TenDangNhap,
                    Program.NguoiSuDung.VaiTroDescription);
                menuHeThong_QuanLyNguoiSuDung.Visible = xoaPTLoiMenu.Visible = Program.NguoiSuDung.VaiTro == NguoiSuDung.VaiTro_DieuHanhTacNghiep;
                #endregion
            }
        }

        private void OnMenuTacNghiep_ThuHocPhiSinhVienClick(object sender, EventArgs e)
        {
            FormThuHocPhiSinhVien frm = new FormThuHocPhiSinhVien(masv: null);
            frm.ShowDialog();
        }

        private void txtSinhVien_Keyword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtSinhVien_Keyword.Text.Length <= 0)
                return;
            if (e.KeyChar == 13)
            {
                var list = BizSinhVien.GetSinhVienByKeyWord(txtSinhVien_Keyword.Text);
                if (list.Count == 0)
                {
                    txtSinhVien_Keyword.Focus();
                }
                else if (list.Count == 1)
                {
                    FormThuHocPhiSinhVien frm = new FormThuHocPhiSinhVien(list[0].MaSinhVien);
                    frm.ShowDialog();
                }
                else
                {
                    FormDanhSachSinhVien frm = new FormDanhSachSinhVien(list);
                    frm.ShowDialog();
                }
            }
        }

        private void btnSinhVien_Search_Click(object sender, EventArgs e)
        {
            txtSinhVien_Keyword_KeyPress(sender, new KeyPressEventArgs((char)13));
        }
        private void OnMenuthongKe_Click(object sender, EventArgs e)
        {

        }

        private void menuDuLieu_Nganh_Click(object sender, EventArgs e)
        {
            //FormChuyenNganh frm = new FormChuyenNganh();
            //frm.ShowDialog();
        }

        private void OnMenuDuLieu_SinhVienChuyenNganhClick(object sender, EventArgs e)
        {

        }

        private void quanLyPhieuThuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            QuanLyPhieuThu qlpt = new QuanLyPhieuThu();
            qlpt.ShowDialog();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ThongTinChuongTrinh frm = new ThongTinChuongTrinh();
            frm.ShowDialog();
        }


        private void cauHinhMenu_Click(object sender, EventArgs e)
        {
            cauHinhHeThong frm = new cauHinhHeThong();
            frm.ShowDialog();
        }

        private void OnMenuThongKeKhongChuyen(object sender, EventArgs e)
        {
            FormThongKe frm = new FormThongKe();
            frm.ShowDialog();
        }

        private void OnMenuThongKeChuyen(object sender, EventArgs e)
        {
            FormThongKeChuyen frm = new FormThongKeChuyen();
            frm.ShowDialog();
        }

        private void OnDanhSachKhongChuyen(object sender, EventArgs e)
        {
            FormTruong frm = new FormTruong();
            frm.ShowDialog();
            frm = null;
        }

        private void OnDanhSachChuyen(object sender, EventArgs e)
        {
            FormSinhVienChuyenNganh frm = new FormSinhVienChuyenNganh();
            frm.ShowDialog();
            frm = null;
        }

        private void OnDanhSachHocPhanKhongChuyen(object sender, EventArgs e)
        {
            TacNghiep.FormQuanLyLopHocPhan frm = new TacNghiep.FormQuanLyLopHocPhan();
            frm.ShowDialog();
            frm = null;
        }

        private void OnDanhSachLopHocPhanChuyenNganh(object sender, EventArgs e)
        {
            FormQuanLyLopHocPhanChuyenNganh frm = new FormQuanLyLopHocPhanChuyenNganh();
            frm.ShowDialog();
        }

        private void thongKeTheoNgay_Click(object sender, EventArgs e)
        {
            FormThongKeTheoNgay frm = new FormThongKeTheoNgay();
            frm.ShowDialog();
        }

        private void menuHeThong_QuanLyNguoiSuDung_Click(object sender, EventArgs e)
        {
            FormQuanLyNguoiSuDung frm = new FormQuanLyNguoiSuDung();
            frm.ShowDialog();
        }

        private void xoaPTLoiMenu_Click(object sender, EventArgs e)
        {
            string rs = "";
            FormProgress frm = new FormProgress();
            frm.DoWork += delegate(object oSender, DoWorkEventArgs oE)
            {
                rs = BizPhieuThu.ListPhieuThuLoi().ToString();
            };
            frm.RunWorkerCompleted += delegate(object oSender, RunWorkerCompletedEventArgs oE)
            {
                MessageBox.Show("Số phiếu thu lỗi đã xóa: " + rs, "Xóa phiếu thu lỗi");
            };
            frm.StartWorking(this);

        }

    }
}
